//
//  AddressScrollView.m
//  GoShop
//
//  Created by mac22 on 14/11/17.
//  Copyright (c) 2014年 郑朝军. All rights reserved.
//

#import "AddressScrollView.h"
#import "Grouble.h"
#import "GetPlistArray.h"

@interface AddressScrollView()
@property (nonatomic,weak)UIScrollView * scrollView;
@end

@implementation AddressScrollView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self initView];
    }
    return self;
}
- (void)initView
{
    UIScrollView * scroller = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 40, ScreenWidth, ScreenHeight*0.5-40)];
    [self addSubview:scroller];
    self.scrollView = scroller;
    
}
-(void)layoutSubviews{
    
    NSArray *array =  [GetPlistArray arrayWithString:@"citydata.plist"];
    NSArray *cityArray =  [array objectAtIndex:16] [@"citylist"];
    NSArray *areaArray = [cityArray objectAtIndex:0][@"arealist"];
    //允许scrollerView竖直移动
    self.scrollView.alwaysBounceVertical = YES;
    //允许scrollerView的可移动内容
    self.scrollView.contentSize = CGSizeMake(ScreenWidth, ScreenHeight-150);
    //三列
    int totalloc = 3;
    
    CGFloat buttonw = 70;
    CGFloat buttonh = 30;
    
    //算出间距
    CGFloat margin=(self.frame.size.width - totalloc * buttonw) / (totalloc + 1);
    //int count=11;
    for (int i = 0; i < areaArray.count; i++) {
        NSDictionary *dict = areaArray[i];
        NSString *titleStr = dict[@"areaName"];
        int row = i / totalloc;//行号
        //0/3=0,1/3=0,2/3=1; (i 从0到2都是第一行) i＝ 3的时候第二行
        
        int loc = i % totalloc;//列号
        
        //横坐标
        CGFloat buttonx = margin + (margin + buttonw) * loc;
        
        //纵坐标
        CGFloat buttony = margin + (margin + buttonh) * row;
        
        
        //创建uiview控件
        UIButton *areaButton=[[UIButton alloc]initWithFrame:CGRectMake(buttonx, buttony, buttonw, buttonh)];
        [areaButton setTitle:titleStr forState:UIControlStateNormal];
        [areaButton setBackgroundColor: ZCJColor(38, 39, 54)];
        [areaButton addTarget:self action:@selector(areaButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollView addSubview:areaButton];
        
    }
    
}

//-(void)areaButtonClick:(UIButton *)button{
//    if ([self.delegate respondsToSelector:@selector(areaButtonClick:)]) {
//        [self.delegate areaButtonClick:button];
//    }
//    
//}




@end
